/*
 * @(#) IndexAction.java 2011-9-6
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.system.presentation.action;

import java.util.ArrayList;
import java.util.List;

import cn.strong.gweva.common.business.entity.ComboBoxEntity;
import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.business.entity.db.JxkhUserEntity;
import cn.strong.gweva.common.execption.ServiceException;
import cn.strong.gweva.common.presentation.action.AjaxBaseAction;
import cn.strong.gweva.common.util.ComboBoxUtil;
import cn.strong.gweva.common.util.StringUtil;
import cn.strong.gweva.common.util.security.Cryptographer;
import cn.strong.gweva.system.business.entity.UserEntity;
import cn.strong.gweva.system.business.service.UserService;

/**
 * 用户管理Action
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-9-6
 */
public class UserAction extends AjaxBaseAction
{

	/** serialVersionUID */
	private static final long serialVersionUID = 5760464117641467312L;
	
	/** 服务 */
	private UserService userService;
	
	/** 检索用户用户信息 */
	private UserEntity user4Search;

	/** 检索结果（分页） */
	private PagerEntity pagerEntity;
	
	/** 用户列表 */
	private List<UserEntity> userList;

	/** 用户信息 */
	private JxkhUserEntity user;
	
	/** 角色列表 */
	private List<ComboBoxEntity> roleList;
	
	/**
	 * 用户列表页面
	 * @return
	 */
	public String list()
	{
		try
		{
			//this.setRoleList(ComboBoxUtil.searchRoleByRight(this.getLoginUser().getRoleId()));

			return this.search();
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 用户新增页面
	 * @return
	 */
	public String add()
	{
		try
		{
			this.setUser(new JxkhUserEntity());
			this.setRoleList(ComboBoxUtil.searchRoleByRight(this.getLoginUser().getRoleId()));

			return ADD;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 用户修改页面
	 * @return
	 */
	public String modify()
	{
		try
		{
			this.setUser(this.userService.searchUserById(this.user.getUserId()));
			if (this.user == null)
			{
				throw new ServiceException("此用户不存在！");
			}
			else
			{
				this.user.setPwd("");
			}
			this.setRoleList(ComboBoxUtil.searchRoleByRight(this.getLoginUser().getRoleId()));

			return MODIFY;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 检索
	 * @return
	 */
	public String search()
	{
		try
		{
			this.setRoleList(ComboBoxUtil.searchRoleByRight(this.getLoginUser().getRoleId()));
			
			this.pagerEntity = this.userService.searchUserPage(this.pagerEntity, this.user4Search);

			return LIST;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete()
	{
		try
		{
			this.userService.deleteById(this.user.getUserId());

			return search();
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 校验用户的存在性。
	 * @return
	 */
	public String checkUserHasExisted()
	{
		try
		{
			writeJson(this.userService.checkUserHasExisted(this.user));
		}
		catch (Exception e)
		{
			return processException(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 插入
	 * @return
	 */
	public String insert()
	{
		try
		{
			this.user.setPwd(Cryptographer.encodeToMD5(this.user.getPwd()));
			this.userService.insert(this.user, this.getLoginUser());
			this.setIsSuccess("保存成功！");

			return add();
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update()
	{
		try
		{
			if (!StringUtil.isEmpty(this.user.getPwd()))
			{
				this.user.setPwd(Cryptographer.encodeToMD5(this.user.getPwd()));
			}
			this.userService.update(this.user, this.getLoginUser());

			return search();
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}

	/**
	 * 获取
	 * @return userService
	 */
	public UserService getUserService()
	{
		return userService;
	}
	/**
	 * 设定
	 * @param userService 
	 */
	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	/**
	 * 获取
	 * @return userList
	 */
	public List<UserEntity> getUserList()
	{
		return userList;
	}
	/**
	 * 设定
	 * @param userList 
	 */
	public void setUserList(List<UserEntity> userList)
	{
		this.userList = userList;
	}

	/**
	 * 获取检索结果（分页）
	 * @return pagerEntity 检索结果（分页）
	 */
	public PagerEntity getPagerEntity()
	{
		return pagerEntity;
	}
	/**
	 * 设定检索结果（分页）
	 * @param pagerEntity 检索结果（分页）
	 */
	public void setPagerEntity(PagerEntity pagerEntity)
	{
		this.pagerEntity = pagerEntity;
	}

	/**
	 * 获取用户信息
	 * @return user 用户信息
	 */
	public JxkhUserEntity getUser()
	{
		return user;
	}
	/**
	 * 设定用户信息
	 * @param user 用户信息
	 */
	public void setUser(JxkhUserEntity user)
	{
		this.user = user;
	}

	/**
	 * 获取角色列表
	 * @return roleList 角色列表
	 */
	public List<ComboBoxEntity> getRoleList()
	{
		if (roleList == null)
		{
			roleList = new ArrayList<ComboBoxEntity>();
		}
		return roleList;
	}
	/**
	 * 设定角色列表
	 * @param roleList 角色列表
	 */
	public void setRoleList(List<ComboBoxEntity> roleList)
	{
		this.roleList = roleList;
	}

	/**
	 * 获取检索用户用户信息
	 * @return user4Search 检索用户用户信息
	 */
	public UserEntity getUser4Search()
	{
		return user4Search;
	}
	/**
	 * 设定检索用户用户信息
	 * @param user4Search 检索用户用户信息
	 */
	public void setUser4Search(UserEntity user4Search)
	{
		this.user4Search = user4Search;
	}
}
